package org.zxstudio.encrypt.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Arrays;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;

/**
 * 封装关于SD卡相关状态函数
 * 
 * @author fan
 * 
 */
public class SDUtils {

	private static final String TAG = "SDUtil";
	private static final boolean DEBUG = true;

	public static String[] TXT_PROJECTIONS = new String[] { "txt" };
	public static String[] IMAGE_PROJECTIONS = new String[] { "jpeg", "jpg",
			"gif" };
	public static String[] AUDIO_PROJECTIONS = new String[] { "mp3" };
	public static String[] VIDEO_PROJECTIONS = new String[] { "mp4", "rmvb",
			"wmv", "rm", "mkv" ,"3gp" };
	public static final String TOKEN = "\\.";

	public static boolean isAvailableCurrentSD() {
		return android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
	}

	public static File getSdRootPath() {
		if (isAvailableCurrentSD()) {
			return Environment.getExternalStorageDirectory();
		} else {
			return null;
		}
	}

	public static boolean isTextFile(File file) {
		List<String> suffixList = Arrays.asList(TXT_PROJECTIONS);
		String[] names = file.getName().toLowerCase().split(TOKEN);
		int length = names.length;
		if (length >= 2) {
			return suffixList.contains(names[length - 1]);
		}
		return false;
	}

	public static boolean isImageFile(File file) {
		List<String> suffixList = Arrays.asList(IMAGE_PROJECTIONS);
		String[] names = file.getName().toLowerCase().split(TOKEN);
		int length = names.length;
		if (length >= 2) {
			return suffixList.contains(names[length - 1]);
		}
		return false;
	}

	public static boolean isImageFile(Context context, String path) {
		ContentResolver contentResolver = context.getContentResolver();
		String[] projection = new String[] { MediaStore.Images.Media._ID,
				MediaStore.Images.Media.TITLE, MediaStore.Images.Media.DATA };
		Cursor cursor = contentResolver.query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
				MediaStore.Images.Media.DATA + " = " + "\"" + path + " \"", null,
				MediaStore.Images.Media.DEFAULT_SORT_ORDER);
		boolean result = (cursor != null && cursor.getCount() > 0) ? true
				: false;
		if (cursor != null)
			cursor.close();
		return result;
	}
	
	

	public static boolean isAudioFile(Context context, String path) {
		ContentResolver contentResolver = context.getContentResolver();
		String[] projection = new String[] { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA };
		Cursor cursor = contentResolver.query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
				MediaStore.Audio.Media.DATA + " = " + " \""+ path + "\"", null,
				MediaStore.Images.Media.DEFAULT_SORT_ORDER);
		boolean result = (cursor != null && cursor.getCount() > 0) ? true
				: false;
		if (cursor != null)
			cursor.close();
		return result;
	}

	public static boolean isAudioFile(File file) {
		List<String> suffixList = Arrays.asList(AUDIO_PROJECTIONS);
		String[] names = file.getName().toLowerCase().split(TOKEN);
		int length = names.length;
		if (length >= 2) {
			return suffixList.contains(names[length - 1]);
		}
		return false;
	}

	public static boolean isVideoFile(File file) {
		List<String> suffixList = Arrays.asList(VIDEO_PROJECTIONS);
		String[] name = file.getName().toLowerCase().split(TOKEN);
		int length = name.length;
		if (length >= 2) {
			return suffixList.contains(name[length - 1]);
		}
		return false;
	}

	public static boolean isVideoFile(Context context, String path) {
		ContentResolver contentResolver = context.getContentResolver();
		String[] projection = new String[] { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATA };
		Cursor cursor = contentResolver.query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection,
				MediaStore.Video.Media.DATA + " = " + "\""+ path+" \"", null,
				MediaStore.Video.Media.DEFAULT_SORT_ORDER);
		boolean result = (cursor != null && cursor.getCount() > 0) ? true
				: false;
		RLog.v(TAG,"isVideoFIle " + result);
		if (cursor != null)
			cursor.close();
		return result;
	}

	/**
	 * 将bm转换为字节数组
	 * 
	 * @param bm
	 * @return
	 */
	public static byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}

	public static Bitmap Bytes2Bimap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		} else {
			return null;
		}
	}

	/**
	 * 获取视频缩略图
	 * 
	 * @param filePath
	 * @return
	 */
	public static Bitmap createVideoThumbnail(String filePath) {
		if (filePath != null) {
			return ThumbnailUtils.createVideoThumbnail(filePath,
					Images.Thumbnails.MICRO_KIND);
		} else {
			return null;
		}
	}

	public static byte[] getVideoThumbnail(String filePath) {
		if (filePath != null) {
			Bitmap bm = createVideoThumbnail(filePath);
			return Bitmap2Bytes(bm);
		} else {
			return null;
		}
	}

	/**
	 * 通过path 获取bm
	 * 
	 * @param filePath
	 * @return
	 */
	public static Bitmap loadImageByPath(String filePath) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.outHeight = 200;
		options.inJustDecodeBounds = true;

		// 获取这个图片的宽和高
		Bitmap bm = BitmapFactory.decodeFile(filePath, options); // 此时返回bm为空
		options.inJustDecodeBounds = false;
		int be = options.outHeight / 20;
		if (be % 10 != 0) {
			be += 10;
		}
		be = be / 10;
		if (be <= 0) {
			be = 1;
		}
		options.inSampleSize = be;
		bm = BitmapFactory.decodeFile(filePath, options);

		int w = bm.getWidth();
		int h = bm.getHeight();
		w = w * 200 / h;
		// view.setImageBitmap(bm);
		// return new BitmapDrawable(bm);
		return bm;
	}

	/**
	 * 获取图片缩略图
	 * 
	 * @param path
	 * @return
	 */
	public static byte[] createImageThumbnail(String path) {
		if (path != null) {
			Bitmap bm = loadImageByPath(path);
			return Bitmap2Bytes(bm);
		} else {
			return null;
		}
	}
}
